﻿using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace System.Modules.SQL.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class DataTableExtensions
    {
        /// <summary>
        /// This method is used to Load all data rows in a data table into an object class.
        /// Root data object class should be implementing IBaseDataObject in order to Load complex properties recursively.
        /// </summary>
        /// <param name="table"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TBaseObject"> </typeparam>
        /// <returns></returns>
        public static List<T> Load<TBaseObject, T>(this DataTable table) where T : new()
        {
            var dataSource = new List<T>();
            if (table != null)
            {
                dataSource.AddRange(from DataRow row in table.Rows select row.Load<TBaseObject,T>());
            }
            return dataSource;
        }
    }
}
